<template>
  <div class="mod-config">
    <el-row>
      <el-form :inline="true" :model="dataForm">
        <el-form-item label="所属店铺">
          <el-popover ref="shopTreePover" 
            placement="bottom-start" 
            trigger="click"
            v-model="shopTreeVisible"
          >
            <el-tree
              :data="shopTree"
              :props="{label: 'name'}"
              node-key="id"
              ref="shopTree"
              @current-change="shopChange"
              :default-expand-all="true"
              :highlight-current="true"
              :expand-on-click-node="false"
            ></el-tree>
          </el-popover>
          <el-input
            v-model="dataForm.shopName"
            v-popover:shopTreePover
            :readonly="true"
            placeholder="选择店铺"
            class="menu-list__input"
          ></el-input>
        </el-form-item>
        <el-form-item label="商品名称">
          <el-input v-model="dataForm.goodsName" placeholder="商品名称搜索" clearable></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="getDataList()">查询</el-button>
          <el-button type="primary" @click="clear()">清除</el-button>
        </el-form-item>
      </el-form>
      <el-table
        :data="dataList"
        border
        v-loading="dataListLoading"
        style="width: 100%;"
        :header-cell-style="{background:'#17b3a3',color:'#fff'}"
      >
        <el-table-column prop="shopName" header-align="center" align="center" label="店铺名称"></el-table-column>
        <el-table-column prop="goods.goodsName" header-align="center" align="center" label="商品名称"></el-table-column>
        <el-table-column prop="goods.catName" header-align="center" align="center" label="商品分类"></el-table-column>
        <!-- <el-table-column prop="goods.goodsUnit" header-align="center" align="center" label="单位"></el-table-column> -->
        <!-- <el-table-column prop="goods.goodsPrice" header-align="center" align="center" label="单价"></el-table-column> -->
        <el-table-column prop="num" header-align="center" align="center" label="库存"></el-table-column>
      </el-table>
      <el-pagination
        @size-change="sizeChangeHandle"
        @current-change="currentChangeHandle"
        :current-page="pageIndex"
        :page-sizes="[10]"
        :page-size="pageSize"
        :total="totalPage"
        layout="total, sizes, prev, pager, next, jumper"
      ></el-pagination>
    </el-row>
  </div>
</template>

<script>
export default {
  data() {
    return {
      shopTreeVisible: false,
      dataForm: {
        companyId: null,
        shopId: null,
        shopName: "",
        goodsName: ""
      },
      companys: [],
      shops: [],
      dataList: [],
      shopTree: [],
      dataListLoading: false,
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
    };
  },
  activated() {
    this.getCompanyShopTree();
    this.getDataList();
  },
  methods: {
    init(){
      this.getDataList()
    },
    clear(){
      this.dataForm = {
        companyId: null,
        shopId: null,
        shopName: "",
        goodsName: ""
      };
      this.getDataList();
    },
    // 获取数据列表
    getDataList() {
      this.dataList = [];
      this.dataListLoading = true;
      this.$http({
        url: this.$http.adornUrl(
          `/merchant/shop/stock/list`
        ),
        method: "get",
        params: this.$http.adornParams({
          page: this.pageIndex,
          limit: this.pageSize,
          shopId: this.dataForm.shopId,
          goodsName: this.dataForm.goodsName
        })
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.dataList = data.page.records;
          this.totalPage = data.page.total;
        } else {
          this.dataList = [];
          this.totalPage = 0;
        }
        this.dataListLoading = false;
      });
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val;
      this.pageIndex = 1;
      this.getDataList();
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val;
      this.getDataList();
    },
    shopChange(data, node) {
      if(node.level != 2){
        this.$message.error("请选择店铺");
        return;
      }
      this.dataForm.shopId = data.id;
      this.dataForm.shopName = data.name;
      this.shopTreeVisible = false;
      this.getDataList()
    },
    getCompanyShopTree() {
      this.shopTree = [];
      this.$http({
        url: this.$http.adornUrl(`/merchant/shop/getCompanyShopTree`),
        method: "get",
        params: this.$http.adornParams({})
      }).then(({ data }) => {
        if (data && data.code === 0) {
          if (data.data && data.data.length > 0) {
            this.shopTree = data.data;
            console.log(this.shopTree);
          }
        }
      });
    }
  }
};
</script>
